package com.example.apidocserver.infrastructure.config;

import com.example.apidocserver.infrastructure.constants.SystemConstants;
import com.example.apidocserver.infrastructure.util.result.ParamException;
import com.example.apidocserver.infrastructure.util.result.ResultData;
import com.example.apidocserver.infrastructure.util.result.ResultUtils;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常捕捉
 *
 * @author xyqlg
 */
@RestControllerAdvice
public class GlobalExceptionHandler {
    /**
     * 参数错误
     *
     * @param paramException 参数异常
     * @return 统一返回格式
     */
    @ExceptionHandler(ParamException.class)
    public ResultData handleException(ParamException paramException) {
        String msg = paramException.getMessage();
        if (SystemConstants.NO_AUTHO.equals(msg)) {
            return ResultUtils.failure(HttpStatus.UNAUTHORIZED.value(), msg);
        }
        return ResultUtils.failure(paramException.getMessage());
    }
}
